﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class ItemsControl : UserControl
    {
        private List<string> messages = new List<string>();
        private int maxNumItems;        
        private SpriteFont font;
        private int topRecord = 0;

        /// <summary>
        /// Selected message from 0-maxnumbermessages (not relative)
        /// </summary>
        private int selectedItem = -1;

        public override int height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
                Vector2 size = TacticsGame.Instance.gameFont.MeasureString(" ");
                maxNumItems = height / ((int)size.Y + Constants.ItemsHeightOffset) - 1;
            }
        }

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureItemsRegular);
            font = TacticsGame.Instance.gameFont;

            width = 100;
            height = 100;

            isDraggable = true;
        }
        
        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (Hover)
            {
                if(InputMgr.Instance.isMouseClickUp(Click.Left))
                {
                    Vector2 size = font.MeasureString(" ");
                    int mx, my;
                    InputMgr.Instance.getMouse(out mx, out my);
                    float unitSize = size.Y;
                    selectedItem = (int)(my/(y + size.Y));
                    //selectedItem = (int)(my / ((size.Y + Constants.ItemsHeightOffset) + Constants.BorderOffset) + y);
                    

                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            int yOffset = Constants.ItemsHeightOffset;
            Vector2 size = font.MeasureString(" ");

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            Rectangle targetRect = new Rectangle(x, y, width, height);

            if (Selected)
            {
                TacticsGame.Instance.spriteBatch.Draw(bgTexture, targetRect, null, Color.White * opacity, 0, Vector2.Zero, SpriteEffects.None, 0);
            }
            else
            {
                TacticsGame.Instance.spriteBatch.Draw(bgTexture, targetRect, null, Color.White * opacity, 0, Vector2.Zero, SpriteEffects.None, 0);                
            }

            int unitHeight = (int)(Constants.BorderOffset + (size.Y + yOffset) * selectedItem);
            TacticsGame.Instance.spriteBatch.Draw(bgTexture, new Rectangle(x, y + unitHeight, width, (int)size.Y), null, Color.Black * 0.5f, 0, Vector2.Zero, SpriteEffects.None, 0); 

            for (int i = 0; i < Math.Min(maxNumItems, messages.Count); i++)
            {
                TacticsGame.Instance.spriteBatch.DrawString(font,
                                            messages[i + topRecord],
                                            new Vector2(x + Constants.BorderOffset, (y + size.Y) * i),
                                            Color.Black * opacity,
                                            0.0f,
                                            new Vector2(0, 0),
                                            Constants.FontScale,
                                            SpriteEffects.None,
                                            0);
            }

            TacticsGame.Instance.spriteBatch.End();
        }

        public void addMessage(string message)
        {
           messages.Add(message);

           if (messages.Count > maxNumItems)
           {
               topRecord = messages.Count - maxNumItems;               
           }
           else
               topRecord = 0;
        }
    

    }
}
